package com.rey.demo.api;

import com.rey.demo.link.ZookeeperDemo;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName: GetApi
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/12/3 12:43
 * @Version 1.0
 */
public class GetApi {
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            // 2.1获取子节点
            assert zooKeeper != null;
            List<String> nodeList = zooKeeper.getChildren("/", false);
            for (String nodeName : nodeList) {
                System.out.println("节点名称："+nodeName);
            }
            // 2.2.获取节点权限信息
            List<ACL> aclList = zooKeeper.getACL("/ro_node", new Stat());
            for (ACL acl : aclList) {
                System.out.println("权限名称:"+acl);
            }
            // 2.3.查看节点数据
            byte[] data = zooKeeper.getData("/ro_node", false, new Stat());
            System.out.println("data:"+new String(data));
            // 2.4.获取客户端状态
            ZooKeeper.States states = zooKeeper.getState();
            System.out.println("zookeeper states:" + states);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
